<?php

namespace App\Http\Controllers\Test;

use App\Model\User;
use App\Services\Base\Rsp;
use App\Services\Constant\ErrorCode;
use App\Services\Dao\SessionDao;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

use Illuminate\Support\Facades\Input;


class ApiController extends Controller
{
    public function anyGetSession(Request $request) {
        if($request->session()->has('user_info')) {
            return Rsp::successMake($request->session()->get('user_info'));
        }

        return Rsp::errorMake(1,'没有用户session信息');
    }



    public function anyTest() {
        return 'test';
    }

    public function anyAddUser(Request $request) {
        $validate = Validator::make($request->all(),[
            'user_name'=>'required | min:3 | unique:users,user_name'
        ]);

        if($validate->fails()) {
            return Rsp::errorMake(ErrorCode::INVALID_PARAMS,$validate->errors()->first());
        }

        $user = new User();

        $user->user_name = Input::get('user_name');
        $user->email = 'email1@test.com';
        $user->user_password = 'password1';

        $user->save();

        return Rsp::successMake($user);
    }

    public function anyList() {
        $limit = Input::get('limit',10);
        $index = Input::get('index',1);

        Paginator::currentPageResolver(function() use ($index) {
            return $index;
        });

        $data = User::paginate($limit)->toArray();
//        $data = DB::table('users')->paginate($limit)->toArray();

        return Rsp::successMake($data);
    }

    public function anyTestUpdateSession() {
        return SessionDao::updateSession('222223','4B6D544A-CE6A-2FBD-0A29-A6DB4E1F63D9');
    }
}
